package net.bwie.gongdan.utils;

import java.io.FileWriter;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

public class DataGenerator {

    private static final int ROW_COUNT = 30; // 生成 30 行数据
    private static final Random random = new Random();

    public static void main(String[] args) {
        List<String[]> data = generateData();
        writeToCSV(data, "sales_data.csv");
    }

    public static List<String[]> generateData() {
        List<String[]> data = new ArrayList<>();
        // 添加表头
        data.add(new String[]{
                "date", "product_id", "visitors", "page_views", "unique_products",
                "avg_stay_duration", "bounce_rate", "collectors", "add_to_cart_items",
                "add_to_cart_users", "order_buyers", "order_items", "order_amount",
                "payment_buyers", "payment_items", "payment_amount",
                "unique_payment_products", "new_payment_buyers", "old_payment_buyers",
                "old_payment_amount", "success_refund_amount", "juhuasuan_payment_amount",
                "yearly_payment_amount", "competitiveness_score", "micro_detail_visitors"
        });

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date startDate = new Date();
        for (int i = 0; i < ROW_COUNT; i++) {
            Date currentDate = new Date(startDate.getTime() + i * 24 * 60 * 60 * 1000);
            String[] row = new String[25];
            row[0] = dateFormat.format(currentDate);
            row[1] = String.valueOf(random.nextInt(1000) + 1); // product_id
            row[2] = String.valueOf(random.nextInt(10000 - 100) + 100); // visitors
            row[3] = String.valueOf(random.nextInt(20000 - 200) + 200); // page_views
            row[4] = String.valueOf(random.nextInt(100 - 10) + 10); // unique_products
            row[5] = String.format("%.2f", random.nextDouble() * (60 - 1) + 1); // avg_stay_duration
            row[6] = String.format("%.2f", random.nextDouble() * (0.9 - 0.1) + 0.1); // bounce_rate
            row[7] = String.valueOf(random.nextInt(500 - 10) + 10); // collectors
            row[8] = String.valueOf(random.nextInt(1000 - 20) + 20); // add_to_cart_items
            row[9] = String.valueOf(random.nextInt(800 - 15) + 15); // add_to_cart_users
            row[10] = String.valueOf(random.nextInt(200 - 5) + 5); // order_buyers
            row[11] = String.valueOf(random.nextInt(500 - 10) + 10); // order_items
            row[12] = String.format("%.2f", random.nextDouble() * (10000 - 100) + 100); // order_amount
            row[13] = String.valueOf(random.nextInt(150 - 4) + 4); // payment_buyers
            row[14] = String.valueOf(random.nextInt(400 - 8) + 8); // payment_items
            row[15] = String.format("%.2f", random.nextDouble() * (8000 - 80) + 80); // payment_amount
            row[16] = String.valueOf(random.nextInt(50 - 5) + 5); // unique_payment_products
            row[17] = String.valueOf(random.nextInt(100 - 2) + 2); // new_payment_buyers
            row[18] = String.valueOf(random.nextInt(100 - 2) + 2); // old_payment_buyers
            row[19] = String.format("%.2f", random.nextDouble() * (5000 - 50) + 50); // old_payment_amount
            row[20] = String.format("%.2f", random.nextDouble() * 1000); // success_refund_amount
            row[21] = String.format("%.2f", random.nextDouble() * 3000); // juhuasuan_payment_amount
            row[22] = String.format("%.2f", random.nextDouble() * (100000 - 10000) + 10000); // yearly_payment_amount
            row[23] = String.format("%.2f", random.nextDouble() * 10); // competitiveness_score
            row[24] = String.valueOf(random.nextInt(2000 - 50) + 50); // micro_detail_visitors
            data.add(row);
        }
        return data;
    }

    public static void writeToCSV(List<String[]> data, String fileName) {
        try (FileWriter writer = new FileWriter(fileName)) {
            for (String[] row : data) {
                for (int i = 0; i < row.length; i++) {
                    writer.append(row[i]);
                    if (i < row.length - 1) {
                        writer.append(",");
                    }
                }
                writer.append("\n");
            }
            System.out.println("数据已成功保存到 " + fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
